<?php
namespace Api\Controller;

/**
 * 分类栏目数据
 * @author Legend. <xcx_legender@qq.com>

function:

index 	首页推荐   			#done
hot   	精选分类			#done
label   分类视频列表
games 	游戏分类 			#done
game 	游戏视频列表 		#done


 * @param
 * @return
 */

class CateController extends PublicController {

	/**
	 * 首页推荐信息
	 * @author Legend. <xcx_legender@qq.com>

	list/:per/:timestamp  按时间戳排序  获取此时间戳之前的N条数据  获取 推送的视频信息
	无per 使用默认  无timestamp 自动取前面的
	< userinfo >

	[ 用户关注人 ] 发布 ( 最新 ) > [ 用户关注游戏 ] 发布 ( 最新 ) > 编辑推荐 > 最新

	#时间戳分页   不计算total  按 > create_time   limit(n) 来做

	 * @param $timestamp 时间戳 该时间之前的数据; $pager 每页的条数 默认
	 * @return
	 */

	public function index() {
		/*$filter_map = array(
			'id','title','thumb','pre_uri', 'uri','time', 'detail','type','create_time','game_id','game','uid','user','statis','tags'
		);*/
		$service = D('Common/Video', 'Service');
		$timestamp = I('timestamp', 0, 'intval');
		$pager = I('pager', C('HOME_VIDEO_PER', NULL, 5), 'intval');
		$uid = $this->getUid(I('appToken'));
		if ($uid) {
			$fEvent = A('Api/Follow', 'Event');
			$sEvent = A('Api/GameScribe', 'Event');
			$follow_follow = A('Api/Follow', 'Event')->mine($uid);
			$follow_game = A('Api/GameScribe', 'Event')->mine($uid);
		} else {
			I('games') && $follow_game = array_map('intval', explode(',', I('games')));
		}
		$query[] = array(
			'recommend' => $service::RECOMMEND_YES,
		);
		!empty($follow_follow) && $query[]['uid'] = array('$in' => $follow_follow);
		!empty($follow_game) && $query[]['game_id'] = array('$in' => $follow_game);
		$map['$and'][]['$or'] = $query;
		$map['$and'][]['create_time'] = $timestamp ? array('$lt' => $timestamp) : array('$gt' => 0);
		$map['$and'][]['status'] = $service::STATUS_PASS;
		$map['$and'][]['race_id'] = array('$eq' => null); ////非比赛视频
		$where['_array'] = $map;
		$res = $service->where($where)->order('create_time desc')->limit($pager)->field('id')->select();
		foreach ($res as $key => $value) {
			$lists[] = $value['id'];
		}
		$item = $this->lists_format($lists);
		$data = $this->formatMVideos($item);

		$this->list_success($data);
	}

	/**
	 * 精选栏目
	 * 找出推荐的TAG和视频信息
	 * @author Legend. <xcx_legender@qq.com>
	 * @param
	 * @return
	 */
	/*  弃用  2016-01-14 20:44:59  Legend.
		public function hot()
		{
			$vEvent = A('Video', 'Event');
			$hot_videos = $vEvent->getHot();
			$hot_videos = $this->lists_format($hot_videos);
			$news_videos = $vEvent->getNew();
			$news_videos = $this->lists_format($news_videos);

			$data[] = array(
				'tag' => '热门推荐',
				'key' => 'hot',
				'lists' => $hot_videos
			);

			$data[] = array(
				'tag' => '最新视频',
				'key' => 'new',
				'lists' => $news_videos
			);

			$tEvent = A('Topic', 'Event');
			// 获取推送Topic
			$topics = $tEvent->getHomeTopics();
			$vEvent = A('Video', 'Event');
			foreach ($topics as $topic) {
				$videos = $topic['videos'];
				if ($videos)
				{
					$videos = explode(',', $videos);
				}
				$videos = $this->lists_format($videos);
				$data[] = array(
					'tag'   => $topic['title'],
					'key'   => $topic['title'],
					'lists' => $videos
				);
			}
			$this->ajaxReturn($data);
		}
	*/

	/**
	 * 分类视频页
	 * @author Legend. <xcx_legender@qq.com>
	 * @param
	 * @return
	 */

	/* 弃用 2016-01-14 20:47:07 Legend.
		public function lists($key = '')
		{
			$service = D('Common/Video', 'Service');
			$map['$and'][]['type'] = $service::TYPE_NORMAL;
			$map['$and'][]['status'] = $service::STATUS_PASS;
			$map['$and'][]['race_id'] = array('$eq'=>null);//非比赛视频  by susy.liang

			$pager 	   = I('pager', C('HOME_VIDEO_PER',NULL, 5), 'intval');
			$order = 'create_time desc';
			$timestamp = I('timestamp',0, 'intval');

			switch ($key) {
				case 'hot':
					// $day = C('HOT_VIDEO_DAYS', NULL, 7);
					I('play') && $map['$and'][]['play'] = array('$lt'=> intval(I('play')));
					$map['$and'][]['play'] = array('$gt' => 0);
					$order = 'play desc';
					break;
				case 'new':
					$timestamp && $map['$and'][]['create_time'] =  array('$lt' => $timestamp);
					break;
				default:
					$tEvent = A('Topic', 'Event');
					$topic  =  $tEvent->info($key, true);
					if (!$topic)
					{
						$this->error(self::$API_ERROR['TOPIC'], '栏目信息错误');
					}
					$map['$and'][]['topics'] = $topic['id'];
					$timestamp && $map['$and'][]['create_time'] =  array('$lt' => $timestamp);
					break;
			}

			$where['_array'] = $map;
			$res = $service->where($where)->order($order)->limit($pager)->field('id')->select();
			foreach ($res as $key => $value) {
				$lists[] = $value['id'];
			}
			// $filter_map = array(
			// 	'id','title','thumb', 'pre_uri', 'uri','time', 'detail','create_time','game_id','game','uid','user','statis','tags'
			// );
			$lists = $this->lists_format($lists);
			$lists = $lists ? $lists : array();
			$this->ajaxReturn($lists);
	*/

	/**
	 * 游戏分类
	 * @author Legend. <xcx_legender@qq.com>
	 * @param
	 * @return
	 */

	public function games() {
		$filter = array(
			'id', 'name', 'pic', 'background', 'scribe', 'is_scribe', 'logo',
		);
		$gEvent = A('Api/Game', 'Event');
		$games = $gEvent->showLists();
		$gsEvent = A('GameScribe', 'Event');
		$uid = $this->getUid(I('appToken'));
		foreach ($games as $key => &$game) {
			uri_format($game['pic']);
			uri_format($game['logo']);
			uri_format($game['background']);
			$uid && $game['is_scribe'] = intval($gsEvent->is_scribe($uid, $game['id']));
			$game['scribe'] = intval($game['scribe']);
			$data[] = field_filter($game, $filter);
		}
		$this->ajaxReturn($data);
	}

	/**
	 * 游戏分类下的视频
	 * @author Legend. <xcx_legender@qq.com>
	 * @param
	 * @return
	 */

	public function game() {
		$filter_map = array(
			'id', 'title', 'thumb', 'action', 'pre_uri', 'uri', 'dpi_levels', 'time', 'detail', 'create_time',
			'game_id', 'uid', 'user', 'statis', 'tags', 'type',
		);
		$gEvent = A('Api/Game', 'Event');
		$game_id = I('id', 0, 'intval');
		if (!$game_id) {
			$this->param_error();
		}
		$game = $this->field_filter($gEvent->info($game_id), array('id', 'pic', 'name', 'background'));
		if (!$game) {
			$this->error(self::$API_ERROR['GAME'], '游戏信息错误');
		}

		uri_format($game['pic']);
		uri_format($game['background']);

		$service = D('Video', 'Service');
		$map = array(); //add 声明 by susy.liang
		$pager = I('pager', C('HOME_VIDEO_PER', NULL, 5), 'intval');
		$pager = 20;
		$timestamp = I('timestamp', 0, 'intval');
		$map['create_time'] = $timestamp ? array('lt', $timestamp) : array('gt', 0);
		$map['game_id'] = $game_id;
		//add type&status param 只取视频类 by susy.liang
		//$map['type'] = $service::TYPE_NORMAL;
		$map['status'] = $service::STATUS_PASS;
		$map['race_id'] = array('egt', null); //非比赛视频
		//end

		$lists = $service->where($map)->order('create_time desc')->limit($pager)->field('id')->select();

		foreach ($lists as $key => $value) {
			$videos[] = $value['id'];
		}
		//start add by susy to get the praise information
		$appToken = I('appToken');
		if ($appToken) {
			$uid = $this->getUid($appToken);
			$likeEvent = A('Api/Like', 'Event');
			foreach ($videos as $key => &$value) {
				$value['action']['is_zan'] = intval($likeEvent->is_like_video($uid, $value['id']));
			}
		}
		//end

		$lists = $this->lists_format($videos, $filter_map);

		$data['game'] = $game;
		$data['lists'] = $lists;
		$data['count'] = count($lists);
		$this->success('', $data);
	}

	/**
	 * 逛一逛
	 * @author Legend. <xcx_legender@qq.com>
	 * @param
	 * @return
	 */

	public function stroll() {
		G('Begin');
		$vEvent = A('Api/Video', 'Event');
		$uid = $this->getUid(I('appToken'));
		$map = [];
		if ($uid) {
			$follow_game = A('Api/GameScribe', 'Event')->mine($uid);
		}else{
			I('games') && $follow_game = array_map('intval', explode(',', I('games')));
		}
		
		isset($follow_game) && $map['game_id'] = ['in', $follow_game];
		$video_ids = $vEvent->randomLists($map);
		$lists = $this->lists_format($video_ids);
		$data = $this->formatMVideos($lists);
		G('end');
		// echo G('Begin', 'end', 7);
		$this->list_success($data);

	}

	######## FROM  2015-09-21 17:30:46 ######

}